package com.haidao.admin.component.web.controller;


import cn.hutool.extra.spring.SpringUtil;
import com.haidao.admin.component.service.ReportService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.Writer;

@Slf4j
@RestController
@RequestMapping("/download")
public class DownLoadController {

    @GetMapping("/data")
    public void report(String tableName, HttpServletResponse response) throws IOException {
        ReportService bean = SpringUtil.getBean(tableName, ReportService.class);
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename=report.csv");
        response.setContentType("application/octet-stream");
        Writer writer = response.getWriter();
        bean.export(writer);
        writer.close();
    }
}

